package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Calendar;
import org.hgvc.webapp.ets.bo.InstallmentSchema;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class InstallmentSchemaBean extends AbstractBean {
	public static final String BEAN_NAME="installmentSchemaBean";
	private MasterSetupService service;
	private List<InstallmentSchema> schemaList;
	private InstallmentSchema schema;
	public static final String NAV_LIST_SCHEMA="listSchema";
	public static final String NAV_ADD_SCHEMA="addSchema";
	public static final String NAV_EDIT_SCHEMA="editSchema";
	private List<SelectItem> depositPercentSelectList;
	private List<SelectItem> mthSelectList;
	 
	public MasterSetupService getService() {
		return service;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}
	
	public InstallmentSchema getSchema() {
		return schema;
	}
	public void setSchema(InstallmentSchema schema) {
		this.schema = schema;
	}
	
	public List<SelectItem> getDepositPercentSelectList() {
		depositPercentSelectList = new ArrayList<SelectItem>();
		for(Object[] array:CommonConstant.INSTALLMENT_SCH_DP_SELECT){
			SelectItem item = new SelectItem(array[0],String.valueOf(array[1]));
			depositPercentSelectList.add(item);
		}
		return depositPercentSelectList;
	}
	public void setDepositPercentSelectList(
			List<SelectItem> depositPercentSelectList) {
		this.depositPercentSelectList = depositPercentSelectList;
	}
	public List<SelectItem> getMthSelectList() {
		mthSelectList = new ArrayList<SelectItem>();
		for(Object[] array:CommonConstant.INSTALLMENT_SCH_MTH_SELECT){
			SelectItem item = new SelectItem(array[0],String.valueOf(array[1]));
			mthSelectList.add(item);
		}
		return mthSelectList;
	}
	public void setMthSelectList(List<SelectItem> mthSelectList) {
		this.mthSelectList = mthSelectList;
	}
	/**
	 * 
	 * 
	 */
	public List<InstallmentSchema> getSchemaList() {
		try{
			schemaList = service.getInstallmentSchemaList();
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return schemaList;
	}
	public void setSchemaList(List<InstallmentSchema> schemaList) {
		this.schemaList = schemaList;
	}
	
	public String navCreationAction(){
		schema = new InstallmentSchema();
		return NAV_ADD_SCHEMA;
	}
	
	public String createAction(){
		try{
			schema.setCreatedBy(getThisUser());
			schema.setCreatedDate(new Date());
			schema.setIsDeleted(CommonConstant.NOT_DELETED);
			service.saveInstallmentSchema(schema);
			return NAV_LIST_SCHEMA;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_ADD_SCHEMA;
	}
	
	public void editListener(ActionEvent actionEvent){
		schema = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("schemaId");
		String sId = (String)parameter.getValue();
		for(InstallmentSchema s:schemaList){
			if(s.getInstallmentSchemaId().equalsIgnoreCase(sId)){
				schema = s;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT_SCHEMA;
	}
	
	public String editAction(){
		try{
			schema.setModifiedBy(getThisUser());
			schema.setModifiedDate(new Date());
			service.updateInstallmentSchema(schema);
			return NAV_LIST_SCHEMA;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return NAV_EDIT_SCHEMA;
	}
	
	public String deleteAction(){
		try{
			List<InstallmentSchema> deleteList = new ArrayList<InstallmentSchema>();
			for(InstallmentSchema s:schemaList){
				if(s.isMarkAsAction()){
					deleteList.add(s);
				}
			}
			service.deleteInstallmentSchema(deleteList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_LIST_SCHEMA;
	}
	
}
